package cn.cocho.android.mc.comp.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.CheckBox;

import cn.cocho.android.mc.R;

/**
 * 自定义复选框
 * 
 * @author HAIKANG SONG
 * @time 2013-5-14下午2:44:16
 */
public class McCheckBox extends CheckBox {

	private static final String CHECKED_ENABLED_IMG = "checkBox_checkedEnabledImg";// 复选框选中时的背景图片
	private static final String CHECKED_DISNABLED_IMG = "checkbox_checkedDisabledImg";// 复选框禁用且选中时的背景图片
	private static final String UNCHECKED_DISNABLED_IMG = "checkbox_uncheckedDisnableImg";// 复选框禁用且未选中时的背景图片

	private Drawable unCheckedEnabledImg;
	private Drawable unCheckedDisnabledImg;
	private Drawable checkedEnabledImg;
	private Drawable checkedDisnabledImg;
	

	private AttributeSet attrs;
	
	private State currentState = State.unchecked_enable;
	private enum State{
		unchecked_enable,
		unchecked_disable,
		checked_enable,
		checked_disable
	};
	
	//*
	public McCheckBox(Context context){
		super(context);
	}

	//*
	public McCheckBox(Context context, AttributeSet attrs, int defsyle){
		super(context, attrs, defsyle);
	}
	
	public McCheckBox(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.attrs = attrs;
//		initCheckBox();
		//*↑
		initCheckBox(context);
	}
	
//	/**
//	 * 初始化复选框
//	 * 
//	 * @author HAIKANG SONG
//	 * @time 2013-5-14下午2:20:44
//	 */
//	public void initCheckBox(){
//		unCheckedEnabledImg = getCompoundDrawables()[0];
//		checkedEnabledImg = getDrawable(checkedEnabledImg, CHECKED_ENABLED_IMG);
//		checkedDisnabledImg = getDrawable(checkedDisnabledImg, CHECKED_DISNABLED_IMG);
//		unCheckedDisnabledImg = getDrawable(unCheckedDisnabledImg, UNCHECKED_DISNABLED_IMG);
//		updateState();
//	}
	
	/**
	 * 初始化复选框
	 * 
	 * @author HAIKANG SONG
	 * @time 2013-5-14下午2:20:44
	 */
	public void initCheckBox(Context context){
		unCheckedEnabledImg = getCompoundDrawables()[0];
		TypedArray tdarr = context.obtainStyledAttributes(attrs, R.styleable.McCheckBox);
		checkedEnabledImg = tdarr.getDrawable(R.styleable.McCheckBox_checkBox_checkedEnabledImg);
		checkedDisnabledImg = tdarr.getDrawable(R.styleable.McCheckBox_checkbox_checkedDisabledImg);
		unCheckedDisnabledImg = tdarr.getDrawable(R.styleable.McCheckBox_checkbox_uncheckedDisnableImg);
		tdarr.recycle();
		updateState();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		if(!isEnabled()){
			return false;
		}
		
		if(isChecked()){
			setChecked(false);
			return false;
		}else{
			setChecked(true);
			return false;
		}
	}
	
	/**
	 * 改变复选框当前的状态
	 * 
	 * @author HAIKANG SONG
	 * @time 2013-5-14下午4:26:05
	 */
	private void updateState(){
		if(!isChecked() && isEnabled()){
			updateCheckBoxImg(State.unchecked_enable, unCheckedEnabledImg);
		}else if(isChecked() && isEnabled()){
			updateCheckBoxImg(State.checked_enable, checkedEnabledImg);
		}else if(!isChecked() && !isEnabled()){
			updateCheckBoxImg(State.unchecked_disable, unCheckedDisnabledImg);
		}else if(isChecked() && !isEnabled()){
			updateCheckBoxImg(State.checked_disable, checkedDisnabledImg);
		}
	}
	
	/**
	 * 修改复选框的图片
	 *
	 * @param state 状态
	 * @param img 图片对象
	 * @author HAIKANG SONG
	 * @time 2013-5-14下午10:51:54
	 */
	private void updateCheckBoxImg(State state, Drawable img) {
		if(currentState != state && img!=null){
			setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
			currentState = state;
		}
	}
	
//	/**
//	 * 获得指定属性背景图片对象
//	 *
//	 * @param drawable 背景图片对象
//	 * @param eventName 属性名称
//	 * @return 
//	 * @author HAIKANG SONG
//	 * @time 2013-5-14下午4:27:26
//	 */
//	private Drawable getDrawable(Drawable drawable, String eventName) {
//		if (drawable == null) {
//			Resources resources = getResources();
//			int drawableId = attrs.getAttributeResourceValue(CompUtils.NS_TBC, eventName, 0);// 读取该属性的值
//			if (drawableId > 0) {
//				drawable = resources.getDrawable(drawableId);
//			} else {
//				drawable = unCheckedEnabledImg;
//			}
//		}
//		return drawable;
//	}
	

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		updateState();
	}
	
	@Override
	public void setChecked(boolean checked){
		super.setChecked(checked);
		updateState();
	}
	
	

	public Drawable getUnCheckedEnabledImg() {
		return unCheckedEnabledImg;
	}

	public void setUnCheckedEnabledImg(Drawable unCheckedEnabledImg) {
		this.unCheckedEnabledImg = unCheckedEnabledImg;
	}

	public Drawable getUnCheckedDisnabledImg() {
		return unCheckedDisnabledImg;
	}

	public void setUnCheckedDisnabledImg(Drawable unCheckedDisnabledImg) {
		this.unCheckedDisnabledImg = unCheckedDisnabledImg;
	}

	public Drawable getCheckedEnabledImg() {
		return checkedEnabledImg;
	}

	public void setCheckedEnabledImg(Drawable checkedEnabledImg) {
		this.checkedEnabledImg = checkedEnabledImg;
	}

	public Drawable getCheckedDisnabledImg() {
		return checkedDisnabledImg;
	}

	public void setCheckedDisnabledImg(Drawable checkedDisnabledImg) {
		this.checkedDisnabledImg = checkedDisnabledImg;
	}
	
	

	
}
